import json
import logging


class HttpErrException(Exception):
    def __init__(self, message, status=400, data={}, level=logging.WARN):
        """
        :param message:
        :param status: http status code
        :param data: 属性名不能叫data，会直接传出用于组织response
        :param level: 即日志的level 常用 info warn error
        """
        self.message = message
        self.status = status
        self.err = data
        self.level = level

    @property
    def full_message(self):
        res = self.message
        if self.err:
            res += ':'
            res += json.dumps(self.err)
        return res

    def __str__(self):
        return self.full_message

    @property
    def code(self):
        return self.status

    @property
    def description(self):
        return self.full_message


class Http200ErrException(Exception):
    def __init__(self, message, status=200, data={}, level=logging.WARN):
        """
        :param message:
        :param status: http status code
        :param data: 属性名不能叫data，会直接传出用于组织response
        :param level: 即日志的level 常用 info warn error
        """
        self.message = message
        self.status = status
        self.err = data
        self.level = level

    @property
    def full_message(self):
        res = self.message
        if self.err:
            res += ':'
            res += json.dumps(self.err)
        return res

    def __str__(self):
        return self.full_message

    @property
    def code(self):
        return self.status

    @property
    def description(self):
        return self.full_message



class RunException(HttpErrException):
    def __init__(self, message, status=500, data={}, level=logging.WARN):
        self.message = message
        self.status = status
        self.err = data
        self.level = level
